package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;

    /**
     * 处理支付超时订单
     */
    @Scheduled(cron = "0 * * * * ? ")
    public void dealTimeOutOrders()
    {
        log.info("定时处理超时订单:{}",LocalDateTime.now());
        LocalDateTime deadLineTime=LocalDateTime.now().plusMinutes(-15);
        List<Orders> list=orderMapper.selectByStatusAndDeadLineTime(Orders.PENDING_PAYMENT,deadLineTime);
        for (Orders orders : list)
        {
            orders.setStatus(Orders.CANCELLED);
            orders.setOrderTime(LocalDateTime.now());
            orders.setCancelReason("超时未支付");
            orderMapper.update(orders);

        }
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void dealDeliveryOrders()
    {
        log.info("定时处理派送中订单:{}",LocalDateTime.now());
        LocalDateTime deadLineTime=LocalDateTime.now().plusHours(-1);
        List<Orders> list=orderMapper.selectByStatusAndDeadLineTime(Orders.DELIVERY_IN_PROGRESS,deadLineTime);
        if(list!=null && list.size()>0)
        {
            for (Orders orders : list) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);

            }
        }

    }
}
